package com.mengmablog.blog.service.impl;

import com.mengmablog.blog.DTO.ArticleInfoDTO;
import com.mengmablog.blog.DTO.MetasCountDTO;
import com.mengmablog.blog.DTO.MetasDTO;
import com.mengmablog.blog.commons.ServerResponse;
import com.mengmablog.blog.dao.ArticlesMapper;
import com.mengmablog.blog.dao.MetasMapper;
import com.mengmablog.blog.dao.MiddlesMapper;
import com.mengmablog.blog.entity.Articles;
import com.mengmablog.blog.entity.Metas;
import com.mengmablog.blog.entity.Middles;
import com.mengmablog.blog.service.port.IMateaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MateaServiceImpl implements IMateaService {
    @Autowired
    private MetasMapper metasMapper;

    @Autowired
    private ArticlesMapper articlesMapper;

    @Autowired
    private MiddlesMapper middlesMapper;

    @Override
    public ServerResponse category_tag(String type) {
        List<Metas> metasList = metasMapper.findByType(type);
        List<MetasDTO> metasDTOList = new ArrayList<>();
        for (Metas metas : metasList) {
            List<Articles> articlesList = articlesMapper.findByNameAndType(metas.getName(), type);
            MetasDTO metasDTO = new MetasDTO();
            BeanUtils.copyProperties(metas, metasDTO);

            List<ArticleInfoDTO> articleInfoDTOList = new ArrayList<>();
            for (Articles articles : articlesList) {
                ArticleInfoDTO articleInfoDTO = new ArticleInfoDTO();
                BeanUtils.copyProperties(articles, articleInfoDTO);
                articleInfoDTOList.add(articleInfoDTO);
            }

            metasDTO.setCount(articlesList.size());
            metasDTO.setArticles(articleInfoDTOList);

            metasDTOList.add(metasDTO);
        }
        return ServerResponse.createBySuccess(metasDTOList);
    }

    @Override
    public ServerResponse meta(String type) {
        List<Metas> metasList = metasMapper.findByType(type);
        List<MetasCountDTO> metasCountList = new ArrayList<>();
        for (Metas metas : metasList) {
            MetasCountDTO metasCountDTO = new MetasCountDTO();
            BeanUtils.copyProperties(metas, metasCountDTO);
            List<Articles> articlesList = articlesMapper.findByNameAndType(metas.getName(),type);
            metasCountDTO.setCount(articlesList.size());
            metasCountList.add(metasCountDTO);
        }
        return ServerResponse.createBySuccess(metasCountList);
    }

    @Override
    public ServerResponse meta_delete(String type, String name) {
        Metas metas = metasMapper.findByTypeAndName(type, name);
        if (metas != null) {
            List<Middles> middles = middlesMapper.findByMId(metas.getId());
            for (Middles middle : middles) {
                Articles articles = articlesMapper.selectByPrimaryKey(middle.getaId());
                if (metas.getType().equals("category")) {
                    if (articles != null) {
                        articles.setCategory(null);
                    }
                }
                if (metas.getType().equals("tag")) {
                    String tag = articles.getTags();
                    String[] strings = tag.split(",");
                    StringBuffer stringBuffer = new StringBuffer();
                    for (String string : strings) {
                        if (!string.equals(name)) {
                            stringBuffer.append(string + ",");
                        }
                    }
                    if (articles != null) {
                        articles.setTags(stringBuffer.toString());
                    }
                }
                articlesMapper.updateByPrimaryKeyWithBLOBs(articles);
                metasMapper.deleteByPrimaryKey(metas.getId());
                middlesMapper.deleteByPrimaryKey(middle.getId());
            }
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    @Override
    public ServerResponse meta_insert(String name, String type) {
        int i = metasMapper.insertMeta(name,type);
        if (i>0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }

    @Override
    public ServerResponse meta_update(String id ,String name, String type) {
        Metas metas = new Metas();
        metas.setId(Integer.valueOf(id));
        metas.setName(name);
        metas.setType(type);
        int i = metasMapper.updateByPrimaryKey(metas);
        if (i>0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }
}
